home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / Search Example / AEVT.c next >
Text File  |  1994-05-17  |  11KB  |  365 lines

  1. /*** AEVT.c ***/
  2.  
  3. /*
  4.   This is an ugly bit of code, hacked together to demonstrate how an application
  5.   can handle the custom event for MacHTTP searches (WWWΩsrch in AppleScript terms).
  6.   
  7.   The only 3 routines of any interest are "HandleSRCH", "HandleSDOC", and "InitAppleEvents."
  8.   They are near the end of this source file. Everything else here is for handling 
  9.   the required suite of events.
  10.   
  11.   The two custom AppleEvents handled are:
  12.   
  13.       Search: Receive a search argument from MacHTTP and return results --ex. Search "some text"
  14.     Search  'char'  -- Search arguments 
  15.     Result:   'char'  -- Search Results
  16.  
  17.      Search Doc: Search a specific document for keywords
  18.     Search Doc  'char'  -- Path Arguments (usually the document to be searched)
  19.         for  'char'  -- Search arguments 
  20.         [address  'char']  -- Client Address
  21.         [user  'char']  -- User Name
  22.         [password  'char']  -- Password
  23.     Result:   'char'  -- Search Results
  24.  
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <AppleEvents.h>
  30.  
  31. #include <Types.h>
  32. #include <Memory.h>
  33. #include <Windows.h>
  34. #include <Dialogs.h>
  35. #include <Menus.h>
  36. #include <Fonts.h>
  37. #include <Events.h>
  38. #include <OSEvents.h>
  39. #include <Desk.h>
  40. #include <ToolUtils.h>
  41.  
  42. #include "AEVT.h"
  43. #include "TTY_Messages.h"
  44. #include "Search.h"
  45.  
  46. extern int done; /*defined in Main.c*/
  47.  
  48. /*******************************************************/
  49.  
  50. OSErr GotRequiredParams( AppleEvent *theAppleEvent )
  51. {
  52. DescType typeCode;
  53. Size actualSize;
  54. OSErr err;
  55.  
  56.     err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  57.                     typeWildCard, &typeCode, nil, 0, &actualSize) ;
  58.     if (err == errAEDescNotFound)
  59.         return noErr;
  60.     else if (err == noErr)
  61.         return errAEEventNotHandled;
  62.     else
  63.         return err;
  64. } /*GotRequiredParams*/
  65.  
  66.  
  67.  
  68. /*******************************************************/
  69.  
  70. /*There are no parameters for an open application event.  So all we need do is open
  71.     an untitled window.  If a reply is sought, the default reply will do.
  72.     Note that this message will normally come only from the Finder */
  73.     
  74. pascal OSErr HandleOAPP( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  75. {
  76. OSErr err;
  77.     TTY_WriteMessage("Open Event");
  78.     /* We don't expect any params at all, but check in case the client requires any */
  79.     err = GotRequiredParams( theAppleEvent ) ;
  80.     if (err != noErr)
  81.         return err;
  82.     else {
  83.         /* Perform open doc ation here*/
  84.         return noErr;
  85.     }
  86. }
  87.  
  88. /*******************************************************/
  89.  
  90. FailErr(OSErr err, char *a)
  91. {
  92. char s[40];
  93.     if (err) {
  94.         sprintf(s, "Error %d: %s\r", err, a);
  95.         TTY_WriteMessage(s);
  96.     }
  97. }
  98.  
  99. /*******************************************************/
  100.  
  101. pascal short MyWaitReplyIdleProc(EventRecord *event, RgnHandle mouseRgn,long sleeptime )
  102. {
  103.     return FALSE;
  104. }
  105.  
  106. /*******************************************************/
  107.  
  108. pascal OSErr HandleQUIT( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  109. {
  110. OSErr err;
  111. char *errStr;
  112. short willQuit;                /* did the user allow the quit or cancel */
  113.  
  114.  
  115.     TTY_WriteMessage("Quit Event");
  116. /* We don't expect any params at all, but check in case the client requires any */
  117.     FailErr( GotRequiredParams( theAppleEvent ), "parms") ;
  118.  
  119.     done = 1;
  120.     if (reply->dataHandle != nil) { /* a reply is sought */
  121.         
  122.         errStr = "yessir; about to quit, sir";
  123.  
  124.         return AEPutParamPtr( reply, 'errs', 'TEXT', errStr, strlen(errStr) );
  125.     }
  126.     else {
  127.         return noErr;
  128.     }
  129. }
  130.  
  131.  
  132. /*******************************************************/
  133.  
  134. pascal OSErr HandleODOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  135. {
  136.     FSSpec myFSS;
  137.     AEDescList docList;
  138.     long index, itemsInList;
  139.     Size actualSize;
  140.     AEKeyword keywd;
  141.     DescType typeCode;
  142.     WindowPtr ignoreWPtr;
  143. char s[80],t[80];
  144.  
  145. /* we are expecting a list of aliases: first get the list into theDesc and check
  146.       that it is the only param the sender considers essential */
  147.     TTY_WriteMessage("Open Doc Event");
  148.  
  149.     FailErr( AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList ), "desc" ) ;
  150.     FailErr( GotRequiredParams( theAppleEvent ), "parms" ) ;
  151.  
  152.     /* now get each alias from the list (as an FSSSpec) and open the associated file. */
  153.  
  154.     FailErr( AECountItems( &docList, &itemsInList ), "count" ) ;
  155.  
  156.     for (index = 1; index<=itemsInList; index++) {
  157.         FailErr( AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,    /*coercion does alias->fsspec */
  158.                 (Ptr) &myFSS, sizeof( myFSS ), &actualSize ), "NthPtr") ;
  159.         
  160.         /** Do Open file stuff ignoreWPtr = OpenFile( &myFSS ) ;**/
  161.         strncpy(t, (char *)&(myFSS.name[1]), myFSS.name[0]);
  162.         t[myFSS.name[0]] = '\0';
  163.         sprintf(s,"Open %s\r",t);
  164.         TTY_WriteMessage(s);
  165.     }
  166.  
  167.  
  168.     FailErr( AEDisposeDesc( &docList ), "dispose" ) ;
  169.     return noErr ;
  170. } /*HandleODOC */
  171.  
  172.  
  173. /*******************************************************/
  174.  
  175. pascal OSErr HandlePDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  176. {    
  177.     FSSpec myFSS;
  178.     AEDescList docList;
  179.     long index, itemsInList;
  180.     Size actSize;
  181.     AEKeyword keywd;
  182.     DescType typeCode;
  183.     WindowPtr ignoreWPtr;
  184.     OSErr err;
  185.     AEDesc ignoreDesc;
  186.     short ignore;
  187.         
  188.     TTY_WriteMessage("Print Doc Event");
  189.     /* we are expecting a list of aliases: first get the list into docList */
  190.  
  191.     FailErr( AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList ), "desc" ) ;
  192.  
  193.     /* now check that all required params have been used: return error if there are any
  194.       that we don't understand */
  195.  
  196.     FailErr( GotRequiredParams( theAppleEvent ), "parms") ;
  197.  
  198.     /* now get each alias (as FSSpec) from the list and open it. */
  199.  
  200.     FailErr( AECountItems( &docList, &itemsInList ), "count" ) ;
  201.  
  202.     for (index = 1; index<=itemsInList; index++) {
  203.  
  204.         FailErr( AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  205.                 (Ptr) &myFSS, sizeof( myFSS ), &actSize ), "Nth" ) ;
  206.  
  207.         /*** Print stuff here PrintFile( &myFSS ) ;**/
  208.     }
  209.     FailErr( AEDisposeDesc( &docList ), "Dispose" ) ;
  210.     
  211.     return noErr;
  212. }
  213.  
  214. /***********************************************/
  215. /* This routine handles the incoming WWWΩsrch AppleEvent */
  216.  
  217. pascal OSErr HandleSRCH( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  218. {    
  219.     Size actSize;
  220.     DescType typeCode;
  221.     char s[1024], t[1024];
  222.     
  223.     TTY_WriteMessage("WWWΩsrch AppleEvent");
  224.  
  225.     /* Get the single direct parameter for this event */
  226.     
  227.     FailErr( AEGetParamPtr( theAppleEvent, keyDirectObject, typeChar, &typeCode,
  228.             (Ptr) s, sizeof( s ), &actSize ), "arg 1" ) ;
  229.             
  230.     /*pretty up the string and show it to the user on the screen*/
  231.     s[actSize] = '\0';
  232.     sprintf(t, "Search Args: %s",s);
  233.     TTY_WriteMessage(t);
  234.     
  235.     /**************************************************************/
  236.     /*This is where any processing, searching, etc. should happen.*/
  237.     sprintf (t, "<h1>%s</h1> This is what you sent!<p>", s);
  238.     
  239.     /*return the result of any processing to the caller (MacHTTP)*/
  240.     if (reply->descriptorType != typeNull) 
  241.         FailErr( AEPutParamPtr(reply, keyDirectObject, typeChar,
  242.             t, strlen (t) ), "Reply");
  243.             
  244.     return noErr;
  245. }
  246.  
  247. /***********************************************/
  248. /* This routine handles the incoming WWWΩsdoc AppleEvent */
  249.  
  250. pascal OSErr HandleSDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  251. {    
  252.     Size actSize;
  253.     DescType typeCode;
  254.     char s[1024], t[1024], out[1024];
  255.     char host[256], user[256], pass[256];
  256.     char *data;
  257.     OSErr err;
  258.     
  259.     TTY_WriteMessage("WWWΩsdoc AppleEvent");
  260.  
  261.     /* Get the direct parameter for this event (path args) */
  262.     FailErr( AEGetParamPtr( theAppleEvent, keyDirectObject, typeChar, &typeCode,
  263.             (Ptr) s, sizeof( s ), &actSize ), "sdoc arg 1" ) ;
  264.  
  265.     s[actSize] = '\0';
  266.             
  267.     /* Get the "for" parameter for this event (search args) */
  268.     FailErr( AEGetParamPtr( theAppleEvent, kForKeyword, typeChar, &typeCode,
  269.             (Ptr) t, sizeof( t ), &actSize ), "sdoc arg 2" ) ;
  270.             
  271.     t[actSize] = '\0';
  272.     
  273.     /**********************************************************/
  274.     /* Note that the following 3 parameters, address, user name, and password,
  275.      * are optional event parameters and may not always be present. If these
  276.      * parameters are missing, the following code initializes them to the null
  277.      * string.
  278.      */
  279.     
  280.     /* Get the "address" parameter for this event*/
  281.     if (err=AEGetParamPtr( theAppleEvent, kAddressKeyword, typeChar, &typeCode,
  282.             (Ptr) host, sizeof( host ), &actSize ))
  283.         actSize = 0;
  284.  
  285.     host[actSize] = '\0';
  286.     
  287.     /* Get the "user name" parameter for this event*/
  288.     if (err=AEGetParamPtr( theAppleEvent, kUserKeyword, typeChar, &typeCode,
  289.             (Ptr) user, sizeof( user ), &actSize ))
  290.         actSize = 0;
  291.     
  292.     user[actSize] = '\0';
  293.     
  294.     /* Get the "password" parameter for this event*/
  295.     if (err=AEGetParamPtr( theAppleEvent, kPasswordKeyword, typeChar, &typeCode,
  296.             (Ptr) pass, sizeof( pass ), &actSize ))
  297.         actSize = 0;
  298.             
  299.     pass[actSize] = '\0';
  300.     
  301.     /*pretty up the strings and show them to the user on the screen*/
  302.     sprintf(out, "Path Args: %s, Search Args: %s, address: %s, user: %s, password: %s", s, t,
  303.         host, user, pass);
  304.     TTY_WriteMessage(out);
  305.     
  306.     /**************************************************************/
  307.     /*This is where any processing, searching, etc. should happen.*/
  308.     /*any security checks based on address, user name, and password may
  309.      * be performed here as well.
  310.      */
  311.     sprintf (out, "<h1>Searching \"%s\" for \"%s\" </h1> This is what you sent!<p>", s, t);
  312.     
  313.     if (data = (char *) NewPtr (32768)) {
  314.     
  315.         if (SearchDocument (s, t, data))
  316.             sprintf (data, "Error, unable to search %s", s);
  317.         
  318.         /*return the result of any processing to the caller (MacHTTP)*/
  319.         if (reply->descriptorType != typeNull) 
  320.             FailErr( AEPutParamPtr(reply, keyDirectObject, typeChar,
  321.                 data, strlen (data) ), "sdoc Reply");
  322.         
  323.         DisposePtr(data);
  324.         
  325.         return noErr;
  326.     }
  327.     else {
  328.         TTY_WriteMessage ("Error: Unable to allocate memory for search data buffer");
  329.         return (-1);
  330.     }
  331. }
  332.  
  333. /***********************************************/
  334. OSErr InitAppleEvents()
  335. {
  336. OSErr aevtErr;
  337.     aevtErr = noErr ;
  338.  
  339.     /*Install handlers for required events*/
  340.     if ( aevtErr == noErr ) 
  341.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  342.                 HandleQUIT, 0, false ) ;
  343.     if ( aevtErr == noErr ) 
  344.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  345.                 HandleOAPP, 0, false ) ;
  346.     if ( aevtErr == noErr ) 
  347.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  348.                 HandleODOC, 0, false ) ;
  349.     if ( aevtErr == noErr ) 
  350.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  351.                 HandlePDOC, 0, false ) ;
  352.                 
  353.     /*install a handler for the MacHTTP search event*/            
  354.     if ( aevtErr == noErr ) 
  355.         aevtErr = AEInstallEventHandler( kMyEventClass, kMyAEWWWSearch,
  356.                 HandleSRCH, 0, false ) ;
  357.  
  358.     /*install a handler for the MacHTTP search doc event*/            
  359.     if ( aevtErr == noErr ) 
  360.         aevtErr = AEInstallEventHandler( kMyEventClass, kMyAEWWWSearchDoc,
  361.                 HandleSDOC, 0, false ) ;
  362.  
  363.     TTY_WriteMessage("Apple Events initialized.\r");
  364.     return aevtErr;
  365. }